package com.gzqylc.docker.extend.exception;

import com.gzqylc.docker.extend.auth.enums.BusinessExceptionEnums;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
 * @Author YeFeng
 * @Date 2021 2021/7/22 下午12:00
 * @Version 1.0
 * 业务类异常断言 业务异常必须存在固定的code
 *  其他异常可以使用Assert进行判断
 *
 */

public class AssertBusiness{

    public static void state(boolean expression, BusinessExceptionEnums businessExceptionEunms) {
        if (!expression) {
            throw new ApiBusinessException(businessExceptionEunms);
        }
    }
    public static void isTrue(boolean expression, BusinessExceptionEnums businessExceptionEunms) {
        if (!expression) {
            throw new ApiBusinessException(businessExceptionEunms);
        }
    }
    public static void isNull(@Nullable Object object, BusinessExceptionEnums businessExceptionEunms) {
        if (object != null) {
            throw new ApiBusinessException(businessExceptionEunms);
        }
    }
    public static void notNull(@Nullable Object object, BusinessExceptionEnums businessExceptionEunms) {
        if (object == null) {
            throw new ApiBusinessException(businessExceptionEunms);
        }
    }
    public static void hasLength(@Nullable String text, BusinessExceptionEnums businessExceptionEunms) {
        if (!StringUtils.hasLength(text)) {
            throw new ApiBusinessException(businessExceptionEunms);
        }
    }

    public static void state(boolean expression, String message) {
        if (!expression) {
            throw new ApiBusinessException(message,"");
        }
    }
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new ApiBusinessException(message,"");
        }
    }

    /**
     * 真的为真
     * @param expression 表达式 为真的时候抛出错误
     * @param message 错误消息
     */
    public static void isActualTrue(boolean expression, String message) {
        if (expression) {
            throw new ApiBusinessException(message,"");
        }
    }

    /**
     * 真的为真
     * @param expression 表达式 为真的时候抛出错误
     * @param exceptionEnums 错误消息
     *
     */
    public static void isActualTrue(boolean expression, BusinessExceptionEnums exceptionEnums) {
        if (expression) {
            throw new ApiBusinessException(exceptionEnums,"");
        }
    }

    public static void isActualTrue(boolean expression, String message,BusinessExceptionEnums exceptionEnums) {
        if (expression) {
            throw new ApiBusinessException(message,exceptionEnums);
        }
    }
    /**
     * 真的为真
     * @param expression 表达式 为真的时候抛出错误
     * @param exceptionEnums 错误消息
     *
     */
    public static void isActualTrue(boolean expression, BusinessExceptionEnums exceptionEnums,String ...target) {
        if (expression) {
            throw new ApiBusinessException(exceptionEnums,target);
        }
    }
    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            throw new ApiBusinessException(message,"");
        }
    }
    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            throw new ApiBusinessException(message,"");
        }
    }
    public static void hasLength(@Nullable String text, String message) {
        if (!StringUtils.hasLength(text)) {
            throw new ApiBusinessException(message,"");
        }
    }

    /**
     * 抛出业务异常
     * @param exceptionEnums
     */
    public static void businessError(BusinessExceptionEnums exceptionEnums){
        throw new ApiBusinessException(exceptionEnums);
    }

    /**
     * 抛出业务异常
     * @param exceptionEnums
     */
    public static void businessError(BusinessExceptionEnums exceptionEnums,String ...target){
        throw new ApiBusinessException(exceptionEnums,target);
    }

    /**
     * 抛出业务异常
     * @param message 使用默认的异常作为抛出
     */
    public static void businessError(String message,String ...target){
        throw new ApiBusinessException(message,target);
    }
}
